package longrise.phone.com.bjjt_jyb.Utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.longrise.android.util.Util;
import com.longrise.android.widget.LButtonBg;
import com.longrise.android.widget.LLinearLayoutView;

import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.View.HeNanDialog;

import static longrise.phone.com.bjjt_jyb.R.color.homePageyellow;


/**
 * Created by Administrator on 2016/5/12. 河南用的uiutils
 */
public class UiUtils {

    private HeNanDialog dialog;
    private static HeNanDialog dialog1;

    /**
     * 双选对话框
     *
     * @param context    上下文
     * @param ResourceId 显示在dialog上的图片的资源文件id
     * @param title      标题
     * @param content    内容
     * @param leftStr    左边btn内容
     * @param rightStr   右边btn内容
     * @param leftClick
     * @param rightClick
     */
    public static HeNanDialog showDialog(Context context, Integer ResourceId, String title, String content, String leftStr, String rightStr,
                                         View.OnClickListener leftClick, View.OnClickListener rightClick) {
        HeNanDialog dialog = null;
        if (null != ResourceId) {
            dialog = new HeNanDialog(context, ResourceId).builder();
        } else {
            dialog = new HeNanDialog(context).builder();
        }
        dialog.setTitle(title);
        dialog.setShowTitle(false);
        dialog.setShowEditText(false);
        dialog.setMsg(content);
        dialog.setNegativeButton(leftStr, leftClick);
        dialog.setPositiveButton(rightStr, rightClick);
        dialog.show();
        return dialog;
    }
    /**
     * 单按钮强制跳转的对话框
     *
     * @param context
     * @param content
     * @param login
     * @param rightClick
     * @return
     */
    public static HeNanDialog showSingleDialog(Context context, Integer ResourceId, String content, String login,
                                               View.OnClickListener rightClick) {
        dialog1 = null;
        if (null != ResourceId) {
            dialog1 = new HeNanDialog(context, ResourceId).builder();
        } else {
            dialog1 = new HeNanDialog(context).builder();
        }
        dialog1.setTitle("提示");
        dialog1.setShowTitle(false);
        dialog1.setShowEditText(false);
        dialog1.setMsg(content);
        dialog1.setPositiveButton(login, rightClick);
        dialog1.setCancelable(false);
        dialog1.show();
        return dialog1;
    }

    public static void missDialog() {
        dialog1.dismiss();
    }

    /**
     * 确定取消提示对话框
     *
     * @param context
     * @param content
     * @param leftClick
     * @param rightClick
     */
    public static HeNanDialog showDialog(Context context, String content, View.OnClickListener leftClick,
                                         View.OnClickListener rightClick) {
        return showDialog(context, null, "提示", content, "取消", "确定", leftClick, rightClick);
    }

    private static TextView textView;

    /**
     * 显示waiting对话框
     *
     * @param text
     */
    public static Dialog showProcessDialogPro(Context context, String text) {

        Dialog processDialog = new Dialog(context, R.style.dialog_style);
        processDialog.setCanceledOnTouchOutside(false);

        LLinearLayoutView dialogView = new LLinearLayoutView(context);
        dialogView.setBackgroundColor(Color.parseColor("#55000000"), -10, -10, -10, -10, Util.dip2px(context, 5), 0,
                Color.parseColor("#55000000"));
        dialogView.setGravity(Gravity.CENTER);
        dialogView.setOrientation(LinearLayout.VERTICAL);

        // 进度条
        ProgressBar bar = new ProgressBar(context);
        bar.setIndeterminate(true);
        bar.setIndeterminateDrawable(context.getResources().getDrawable(R.drawable.lcontainerview_anim_loading));
        int w = Util.dip2px(context, 30);
        dialogView.addView(bar, w, w);

        textView = new TextView(context);
        textView.setText(text);
        textView.setGravity(Gravity.CENTER);
        textView.setTextSize(14);
        textView.setTextColor(Color.parseColor("#FFFFFF"));
        textView.setPadding(0, Util.dip2px(context, 10), 0, 0);
        dialogView.addView(textView);

        int widthDialog = Util.dip2px(context, 100);
        LinearLayout.LayoutParams dialogLP = new LinearLayout.LayoutParams(widthDialog, widthDialog);
        processDialog.setContentView(dialogView, dialogLP);
        // add by huxin
        processDialog.show();
        //
        dialogLP = null;
        return processDialog;
    }

    public static void changeText(String text) {
        textView.setText(text);
    }

    /**
     * Windows中的悬浮窗用于application
     */

    public static AlertDialog showWindowDIalog(Context context, String title, String message, String negebutton,
                                               String posibutton, DialogInterface.OnClickListener listener) {
        AlertDialog alertDialog = showMainDialog(context, title, message, negebutton, posibutton, listener);
        alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        //系统界别的dialog，即全局性质的dialog。这种dialog在任何界面下都可以弹出来

        return alertDialog;
    }
    /**
     * 返回一个AlertDialog对象，外界操作
     *
     * @param context
     * @param title
     * @param message
     * @param negebutton
     * @param posibutton
     * @return
     */
    public static AlertDialog showMainDialog(Context context, String title, String message, String negebutton,
                                             String posibutton, DialogInterface.OnClickListener listener) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context, R
                .style.AlertDialogStyle);
        AlertDialog alertDialog = builder.setTitle(title)
                .setMessage(message)
                .setNegativeButton(negebutton, null)
                .setPositiveButton(posibutton, listener)
                .create();

        return alertDialog;
    }

    /**
     * 该业务已上传，提示是否继续上传
     */
    public static void showRemarksDialog(Context context,String remarks) {
        if (null == context ) {
            return;
        }
        // 自定义对话框
        final Dialog exitDialog = new Dialog(context, R.style.dialog_style);
        exitDialog.setCanceledOnTouchOutside(false);
        exitDialog.setCancelable(false);

        // ==================对话框布局==================
        int roundRadius = Util.dip2px(context, 10);
        LLinearLayoutView dialogBody = new LLinearLayoutView(context);
        dialogBody.setOrientation(LinearLayout.VERTICAL);
        dialogBody.setBackgroundColor(Color.parseColor("#ffffff"), Color.parseColor("#ffffff"),
                Color.parseColor("#ffffff"), Color.parseColor("#ffffff"), Color.parseColor("#ffffff"), roundRadius, 0,
                Color.parseColor("#ffffff"));

        // 标题
        int pad = Util.dip2px(context, 10);
        TextView setTitle = new TextView(context);
        setTitle.setGravity(Gravity.CENTER);
        setTitle.setText("亲，请关注增加的功能");
        setTitle.setTextSize(20);
        setTitle.setTextColor(context.getResources().getColor(homePageyellow));
        setTitle.setPadding(0, pad, 3, pad);
        dialogBody.addView(setTitle, LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        // ------------主体-------------
        LLinearLayoutView bodyLayout = new LLinearLayoutView(context);
        bodyLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        bodyLayout.setOrientation(LinearLayout.VERTICAL);
        bodyLayout.setBackgroundColor2(Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE, Color.WHITE,
                new float[]{0, 0, 0, 0, roundRadius, roundRadius, roundRadius, roundRadius}, 0,
                Color.parseColor("#55E0E0E0"));
        dialogBody.addView(bodyLayout, (int) (Util.getScreenWidth(context) * 0.8),
                LinearLayout.LayoutParams.WRAP_CONTENT);
        int padNoticeStr = Util.dip2px(context, 20);
        EditText editText = null;

        TextView noticeStr = new TextView(context);
        noticeStr.setPadding(padNoticeStr, 0, padNoticeStr, 0);
        noticeStr.setBackgroundColor(Color.WHITE);
        //noticeStr.setGravity(Gravity.CENTER);
        noticeStr.setText(remarks);
        noticeStr.setTextSize(17);
        noticeStr.setTextColor(Color.parseColor("#AA000000"));
        LinearLayout.LayoutParams noticeStrLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        noticeStrLP.weight = 1;
        bodyLayout.addView(noticeStr, noticeStrLP);
        noticeStrLP = null;

        // 分隔线
        View line = new View(context);
        line.setBackgroundColor(Color.parseColor("#E0E0E0"));
        bodyLayout.addView(line, ViewGroup.LayoutParams.MATCH_PARENT, 1);

        LinearLayout btnLayout = new LinearLayout(context);
        bodyLayout.addView(btnLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        float[] myRoundRadius = null;
        // 強制更新，隐藏取消按钮
        myRoundRadius = new float[]{0, 0, 0, 0, roundRadius, roundRadius, roundRadius, roundRadius};
        // 确定按钮
        LButtonBg setOK = new LButtonBg(context);
        setOK.setBackgroundColor2(Color.WHITE, Color.parseColor("#E0E0E0"), Color.parseColor("#E0E0E0"), -10,
                Color.parseColor("#E0E0E0"), myRoundRadius, 0, Color.parseColor("#E0E0E0"));
        setOK.setText("确定");
        setOK.setTextColor(Color.parseColor("#299de5"));
        setOK.setTextSize(15);
        LinearLayout.LayoutParams setOKLP = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                Util.dip2px(context, 45));
        setOKLP.weight = 1;
        btnLayout.addView(setOK, setOKLP);
        setOK.setTag(editText);
        setOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                exitDialog.dismiss();
            }
        });
        exitDialog.setContentView(dialogBody);
        exitDialog.show();
    }
}
